package com.uniledger.exchange.mine.scheme;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uniledger.commonlib.util.TimeConverterUtil;
import com.uniledger.exchange.R;
import com.uniledger.exchange.glide.GlideApp;
import com.uniledger.exchange.model.TrxHistory;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<TrxHistory> list;
    private Context context;
    private LayoutInflater inflater;

    public HistoryAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
        this.inflater = LayoutInflater.from(context);
    }

    public void addData(List<TrxHistory> histories) {
        this.list.addAll(histories);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.list_item_history, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TrxHistory history = list.get(position);
        VH vh = (VH) holder;
        if (history.cashflow == 0) {//支出
            String descString = history.getCategory(context);
            vh.desc.setText(descString);
            Drawable drawable = context.getResources().getDrawable(R.drawable.ic_red_aed);
            vh.txt_symbol.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
            vh.txt_symbol.setText("-");
            vh.txt_symbol.setTextColor(Color.parseColor("#F82B29"));
            vh.amount.setText(history.getSrcAmount());
            vh.amount.setTextColor(Color.parseColor("#F82B29"));
        } else {//进账
            Drawable drawable = context.getResources().getDrawable(R.drawable.ic_green_aed);
            vh.txt_symbol.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
            vh.txt_symbol.setText("+");
            vh.txt_symbol.setTextColor(Color.parseColor("#039F15"));
            vh.amount.setText(history.getSrcAmount());
            vh.amount.setTextColor(Color.parseColor("#039F15"));
            String descString = history.getCategory(context);
            vh.desc.setText(descString);
        }
        vh.status.setText(history.state);
        GlideApp.with(context).load(history.contact.getAvatar())
                .placeholder(R.drawable.head)
                .error(R.drawable.head)
                .circleCrop()
                .into(vh.logo);

        vh.time.setText(TimeConverterUtil.utc2Local(history.datetime, "yyyy-MM-dd'T'HH:mm:ss'Z'", "dd MMM',' yyyy HH:mm"));

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class VH extends RecyclerView.ViewHolder {

        @BindView(R.id.imageView)
        CircleImageView logo;

        @BindView(R.id.txt_desc)
        TextView desc;

        @BindView(R.id.txt_amount)
        TextView amount;

        @BindView(R.id.txt_time)
        TextView time;

        @BindView(R.id.txt_status)
        TextView status;

        @BindView(R.id.txt_symbol)
        TextView txt_symbol;

        public VH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
